package com.example.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.common.result.Result;
import com.example.backend.dto.BookDTO;
import com.example.backend.service.BookService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookController {
    
    private final BookService bookService;
    
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }
    
    @GetMapping("/page")
    public Result<Page<BookDTO>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        Page<BookDTO> page = bookService.page(current, size, keyword);
        return Result.success(page);
    }
    
    @GetMapping("/{id}")
    public Result<BookDTO> getById(@PathVariable Long id) {
        BookDTO bookDTO = bookService.getById(id);
        return Result.success(bookDTO);
    }
    
    @PostMapping
    public Result<Void> save(@RequestBody BookDTO bookDTO) {
        bookService.save(bookDTO);
        return Result.success();
    }
    
    @PutMapping
    public Result<Void> update(@RequestBody BookDTO bookDTO) {
        bookService.update(bookDTO);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        bookService.delete(id);
        return Result.success();
    }
} 